//
//  TVCardCellModel.swift
//  StarTimesON
//
//  Created by Toj on 8/24/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

class TVCardCellModel: NSObject {
    
    var id: String = ""
    // 卡号
    private(set) var cardNumber = ""
    private(set) var cardNumberFormat = ""
    
    // 信号播放来源
    private(set) var products: [String: ProductVO] = [:]
    
    // 卡状态
    private(set) var cardState: DVBSmartCardStatus = .default
    private var _stateText = ""
    var stateString: String {
        if !_stateText.isEmpty { return _stateText }
        
        var stateDescription: String = ""
        switch cardState {
        case .punishStop:
            stateDescription = OLLocalize("dormant")
        case .pause:
            stateDescription = OLLocalize("link_suspend")
        case .invalid:
            break
        default:
            if !penaltyStop.isEmpty {
                var time = penaltyStop
                let formatter = DateFormatter()
                formatter.dateFormat = "yyyyMMdd"
                formatter.locale = Locale(identifier: "en")
                
                if let date = formatter.date(from: penaltyStop) {
                    formatter.dateFormat = "MMM.dd yyyy"
                    time = formatter.string(from: date)
                }
                
                stateDescription = "link_active_to".localized() + String(format: " %@", time)
            } else {
                stateDescription = OLLocalize("smart_card_balance") + String.currencyFormate(sign: STSPayAccount.current.currencySign, number: self.money)
            }
        }
        _stateText = stateDescription
        return stateDescription
    }
    
    // 提示
    private(set) var tips = ""
    
    // 罚停日期
    private var penaltyStop = ""
    private var money = 0.0
    
    private override init() { }
    public init(card number: String) {
        cardNumber = number
    }
    
    public func reloadCard(linked: DVBSmartCardDetail) {
        cardNumber = linked.smardCardNo
        cardNumberFormat = cardNumber.st_numberFormat()
        
        cardState = linked.smartCardStatus
        tips = cardState.tips.localized()
        // 罚停日期
        penaltyStop = linked.penaltyStop
        money = linked.money
        
        var signals: [String: ProductVO] = [:]
        linked.products.forEach { product in
            guard signals[product.tvPlatform.rawValue] == nil else { return }
            signals[product.tvPlatform.rawValue] = product
        }
        products = signals
    }
    
    public init(actived package: ActiveServiceInstant) {
        super.init()
        cardNumber = package.smartcard
        cardState = .invalid
        tips = ""
    }
    
    public init(card linked: DVBSmartCardDetail) {
        super.init()
        reloadCard(linked: linked)
    }
    
    public func reloadCard(card: DVBSmartCardInfo) {
        cardNumber = card.smartCardNo
        cardNumberFormat = cardNumber.st_numberFormat()
        
        cardState = card.smartCardStatus
        // 罚停日期
        penaltyStop = card.penaltyStop
        money = card.money
        
        var signals: [String: ProductVO] = [:]
        card.products.forEach { product in
            guard signals[product.tvPlatform.rawValue] == nil else { return }
            signals[product.tvPlatform.rawValue] = product
        }
        products = signals
    }
    
    public init(card linked: DVBSmartCardInfo) {
        super.init()
        reloadCard(card: linked)
    }
}
